﻿

using System.Linq;
using System.Collections.Generic;
using System.Data;
using System.Reflection.Emit;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;

namespace CQIE.LabAssist.Models
{
    public class _DbContext : DbContext
    {

        /// <summary>
        /// 表
        /// </summary>
        
        //用户表
        public DbSet<User> Users { get; set; }
        //角色表
        public DbSet<Role> Roles { get; set; }
        //用户角色连接表
        public DbSet<UserRole> UserRoles { get; set; }
        //功能表
        public DbSet<Menu> Menus { get; set; }
        //角色功能表
        public DbSet<RoleMenue> RoleMenues { get; set; }
        //学期表
        public DbSet<Semester> Semesters { get; set; }
        //学院表
        public DbSet<Academician> Academicians { get; set; }
        //楼栋表
        public DbSet<Building> Buildings { get; set; }
        //楼层表
        public DbSet<Floor> Floors { get; set; }
        //实验室表
        public DbSet<Laboratory> Laboratories { get; set; }
        //检查表
        public DbSet<Check> Checks { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.ApplyConfigurationsFromAssembly(this.GetType().Assembly);

            //角色与权限配置
            modelBuilder.Entity<RoleMenue>()
             .HasKey(rm => new { rm.SysMenuId, rm.RoleId });

            ///用户与角色配置
            modelBuilder.Entity<UserRole>()
                .HasKey(ur => new { ur.UserId, ur.RoleId });

            ///用户与学院配置
            modelBuilder.Entity<Academician>()
                .HasMany(ac=>ac.users)
                .WithOne(u=>u.Academician)
                .HasForeignKey(b => b.acadminianId);

            ///楼栋与楼层配置
            modelBuilder.Entity<Building>()
               .HasMany(b => b.floors)
               .WithOne(f => f.building)
               .HasForeignKey(f => f.buildingId);

            ///楼层与实验室配置
            modelBuilder.Entity<Floor>()
               .HasMany(f => f.laboratories)
               .WithOne(l => l.floor)
               .HasForeignKey(l => l.floorId);

            ///检查表与实验室
            modelBuilder.Entity<Check>()
                .HasOne(c => c.laboratory)
                .WithMany(l => l.checks)
                .HasForeignKey(c => c.laboratoryId);

            ///检查表与学期
            modelBuilder.Entity<Check>()
               .HasOne(c => c.semester)
               .WithMany(l => l.checks)
               .HasForeignKey(c => c.semesterId);
        }



        /// <summary>
        /// 数据库连接
        /// </summary>
        /// <param name="optionsBuilder"></param>
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            //获取appsetting文件的连接字符串
            var builder = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("appsettings.json");
            var configuration = builder.Build();
            string connectString = configuration["DBConnectionStrings:ManagerDB:ConnectionString"]!;
            base.OnConfiguring(optionsBuilder);
            var serverVersion = new MySqlServerVersion(new Version(8, 0, 34));
            optionsBuilder.UseMySql(connectString, serverVersion);
        }


    }
}
